package com.lmpop.createIndex.search;

import com.lmpop.createIndex.searchModel.Queue;
import com.lmpop.createIndex.searchModel.QueueValue;
import com.lmpop.createIndex.searchModel.SearchData;
import com.lmpop.createIndex.searchModel.SearchModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class GoodsIndexFiles
{
  public ProperUtil properUtil;
  private String file_path;

  public GoodsIndexFiles()
  {
    this.properUtil = new ProperUtil();
    this.file_path = this.properUtil.getFilePathByModel("goods");
  }

  public Map getOneGoods(String goods_id) throws IOException
  {
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "goods_id", goods_id));

    return search.getSearchObject(this.file_path, paraList);
  }

  public Map getOneGoods(String goods_id, String filepath) throws IOException {
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "goods_id", goods_id));

    if ((filepath != null) && (!"".equals(filepath)))
      this.file_path = this.properUtil.getFilePathByModel(filepath);
    return search.getSearchObject(this.file_path, paraList);
  }

  public List getGoodsInfo(Map paraMap) throws IOException {
    ArrayList showList = new ArrayList();
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();

    String cat_id = ""; String keyword = ""; String recommend = ""; String orderby = "in_date"; String brand_id = "";
    String start_price = ""; String end_price = ""; String attr_desc_para = ""; String info_state = ""; String cust_id = ""; String state_code = ""; String e = "e"; String f = "f"; String g = "g";
    boolean sort_type = true;
    if (paraMap.get("info_state") != null) {
      info_state = paraMap.get("info_state").toString();
    }
    if (paraMap.get("cust_id") != null) cust_id = paraMap.get("cust_id").toString();
    if (paraMap.get("cat_id") != null) cat_id = paraMap.get("cat_id").toString();
    if (paraMap.get("brand_id") != null) brand_id = paraMap.get("brand_id").toString();
    if (paraMap.get("recommend") != null) recommend = paraMap.get("recommend").toString();
    if (paraMap.get("keyword") != null) keyword = paraMap.get("keyword").toString();
    if (paraMap.get("start_price") != null) start_price = paraMap.get("start_price").toString();
    if (paraMap.get("end_price") != null) end_price = paraMap.get("end_price").toString();
    if (paraMap.get("attr_desc_para") != null) attr_desc_para = paraMap.get("attr_desc_para").toString();
    if (paraMap.get("orderby") != null) orderby = paraMap.get("orderby").toString();
    if (paraMap.get("sort_type") != null) sort_type = new Boolean(paraMap.get("sort_type").toString()).booleanValue();
    if (paraMap.get("state_code") != null) state_code = paraMap.get("state_code").toString();
    Queue queue = new Queue(new QueueValue[] { new QueueValue(orderby, 2, sort_type) });

    attr_desc_para = attr_desc_para.replace(",", "|");

    if ((!start_price.equals("")) && (!end_price.equals(""))) {
      start_price = addZeroByLeft(start_price);
      end_price = addZeroByLeft(end_price);
      paraList.add(new SearchModel("3", "_sale_price", start_price, end_price));
    }
    if (info_state.equals("e"))
      paraList.add(new SearchModel("0", "e", info_state));
    if (info_state.equals("f"))
      paraList.add(new SearchModel("0", "f", info_state));
    if (info_state.equals("g"))
        paraList.add(new SearchModel("0", "g", info_state));
	    paraList.add(new SearchModel("0", "cust_id", cust_id));
	    paraList.add(new SearchModel("0", "class_attr", cat_id));
	    paraList.add(new SearchModel("0", "brand_id", brand_id));
	    paraList.add(new SearchModel("0", "state_code", state_code));
	    paraList.add(new SearchModel("0", "recommend", recommend));
	    paraList.add(new SearchModel("2", keyword, new String[] { "goods_name", "keywords" }));
    if (paraMap.get("file_path") != null)
      this.file_path = this.properUtil.getFilePathByModel(paraMap.get("file_path").toString());
    showList = search.getSearchInfo(this.file_path, paraList, queue);

    showList = filterAttrdescList(attr_desc_para, showList);

    return showList;
  }

  public ArrayList filterAttrdescList(String attr_desc_para, ArrayList infoList)
  {
    if (infoList == null) return null;
    if (attr_desc_para.equals("")) return infoList;
    ArrayList thisList = new ArrayList();
    String[] attr_str = attr_desc_para.split("\\|");
    if ((attr_str.length > 0) && (infoList != null) && (infoList.size() > 0)) {
      Hashtable sMap = new Hashtable();
      for (int i = 0; i < infoList.size(); i++) {
        sMap = (Hashtable)infoList.get(i);
        String attr_desc = "";
        if (sMap.get("attr_desc") != null) attr_desc = sMap.get("attr_desc").toString().trim();
        if (!attr_desc.equals("")) {
          int b = 0;
          for (int j = 0; j < attr_str.length; j++) {
            if ((!attr_str[j].equals("")) && (attr_desc.indexOf(attr_str[j]) > -1)) {
              continue;
            }
            b++;
          }

          if (b == 0) {
            thisList.add(sMap);
          }
        }
      }
    }
    if (infoList != null) infoList.clear();
    infoList = null;
    return thisList;
  }

  public String addZeroByLeft(String price) {
    if (price.equals("")) return "";
    int price_len = price.length();
    int add_len = 0;
    if (price_len < 10) {
      add_len = 10 - price_len;
    }
    String zeroStr = "";
    for (int i = 0; i < add_len; i++) {
      zeroStr = zeroStr + "0";
    }
    return zeroStr + price;
  }

  public Map getGoodsPriceMaxMin(List goodsList) {
    Map mmMap = new Hashtable();
    int max = 0; int min = 0;
    if ((goodsList != null) && (goodsList.size() > 0)) {
      Hashtable gMap = new Hashtable();
      for (int i = 0; i < goodsList.size(); i++) {
        gMap = (Hashtable)goodsList.get(i);
        int _sale_price = 0;
        if ((gMap.get("_sale_price") != null) && (!gMap.get("_sale_price").toString().equals(""))) _sale_price = Integer.parseInt(gMap.get("_sale_price").toString());
        if (max == 0) max = _sale_price;
        if (min == 0) min = _sale_price;
        if (_sale_price > max) {
          max = _sale_price;
        }
        if (_sale_price < min) {
          min = _sale_price;
        }
      }
      mmMap.put("max", String.valueOf(max));
      mmMap.put("min", String.valueOf(min));
    }
    return mmMap;
  }

  public List getGoodsCatListNum(String up_cat_id, List goodsList) throws IOException {
    List numList = new ArrayList();
    CategoryIndexFiles catIndex = new CategoryIndexFiles();
    ArrayList catList = catIndex.getCategoryByUpCatId(up_cat_id, "2");
    if ((catList != null) && (catList.size() > 0)) {
      Hashtable catMap = new Hashtable();
      for (int i = 0; i < catList.size(); i++) {
        catMap = (Hashtable)catList.get(i);
        String cat_id = ""; String cat_name = "";
        if (catMap.get("cat_id") != null) cat_id = catMap.get("cat_id").toString();
        if (catMap.get("cat_name") != null) cat_name = catMap.get("cat_name").toString();
        int cat_num = catEixstNum(cat_id, goodsList);
        if (cat_num > 0) {
          Hashtable tempMap = new Hashtable();
          tempMap.put("cat_num", String.valueOf(cat_num));
          tempMap.put("cat_id", cat_id);
          tempMap.put("cat_name", cat_name);
          numList.add(tempMap);
        }
      }
    }
    return numList;
  }

  public int catEixstNum(String cat_id, List goodsList) {
    int num = 0;
    if ((goodsList != null) && (goodsList.size() > 0)) {
      Hashtable gMap = new Hashtable();
      for (int i = 0; i < goodsList.size(); i++) {
        gMap = (Hashtable)goodsList.get(i);
        String class_attr = "";
        if (gMap.get("class_attr") != null) class_attr = gMap.get("class_attr").toString();
        if (class_attr.indexOf(cat_id) <= -1) continue; num++;
      }
    }
    return num;
  }

  public String dealAttrdesc(String attr_desc_para) {
    String[] cat_str = attr_desc_para.split(",");
    Hashtable bMap = new Hashtable();
    for (int i = 0; i < cat_str.length; i++) {
      String tagStartBody = cat_str[i];
      tagStartBody = tagStartBody.replace(",", "");
      if (tagStartBody.indexOf("|") > -1) {
        String[] ess = tagStartBody.split("\\|");
        bMap.put(ess[0], ess[1]);
      }
    }
    String cat_desc = bMap.toString();
    cat_desc = cat_desc.replace("=", "|").replace(" ", "").replace("{", "").replace("}", ",");
    if (cat_desc.equals(",")) {
      cat_desc = "";
    }
    return cat_desc;
  }
}